import os, shutil, sys

sys.path.append('./utils')
import JsonLoader
import StringUtils
import stat
import MySQLdb

def update_drupal_dbinfo(drupal_path,  drupal_db_str):
    settings_file = os.path.join(drupal_path, 'sites/default/settings.php') 
    
    fH = open(settings_file,'r')
    text = fH.read()
    fH.close()
    
    text_new = StringUtils.ReplaceText( text, {'mysql://username:password@localhost/databasename':drupal_db_str})
    
    fH = open(settings_file, 'w')    
    fH.write(text_new)
    fH.close()

def force_remove(top):
    # Delete everything reachable from the directory named in "top",
    # assuming there are no symbolic links.
    # CAUTION:  This is dangerous!  For example, if top == '/', it
    # could delete all your disk files.
    for root, dirs, files in os.walk(top, topdown=False):
        for name in files:
            filepath = os.path.join(root, name)
            try:
                os.remove(filepath)
            except OSError, e1:
                if e1.errno == 13:
                    print "A read-only file, force to writable" 
                    os.chmod(filepath, stat.S_IWRITE)
                    os.remove(filepath)
                else:
                    raise                
            print "File removed ", filepath
            
        for name in dirs:
            dirpath = os.path.join(root, name)
            os.rmdir(dirpath)
            print "Dir removed ", dirpath
    # don't forget to remove top        
    try:
        os.rmdir(top)
    except OSError, e3:
        print "Error ", e3.errno, e3.strerror
        # list out content of top at the time        
        os.chmod(top, stat.S_IWRITE)
        os.rmdir(top)
    print "Dir removed ", top
    
def install_system(CONFIG_DBSERVER, CONFIG_SITE):
    # the content of this function is from system.install
    connection = MySQLdb. connect (host   = CONFIG_DBSERVER['host'],
                                   db     = CONFIG_SITE['db']['dbname'],
                                   user   = CONFIG_SITE['db']['username'],
                                   passwd = CONFIG_SITE['db']['password'])
    cursor = connection.cursor()
    
    # Note: table names can be prefixed
    cursor.execute("""
        CREATE TABLE access (
        aid int NOT NULL auto_increment,
        mask varchar(255) NOT NULL default '',
        type varchar(255) NOT NULL default '',
        status tinyint NOT NULL default '0',
        PRIMARY KEY (aid)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)
    print cursor.fetchall()
   
    cursor.execute("""
        CREATE TABLE authmap (
        aid int unsigned NOT NULL auto_increment,
        uid int NOT NULL default '0',
        authname varchar(128) NOT NULL default '',
        module varchar(128) NOT NULL default '',
        PRIMARY KEY (aid),
        UNIQUE KEY authname (authname)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)
    print cursor.fetchall()

    cursor.execute("""
        CREATE TABLE blocks (
        module varchar(64) DEFAULT '' NOT NULL,
        delta varchar(32) NOT NULL default '0',
        theme varchar(255) NOT NULL default '',
        status tinyint DEFAULT '0' NOT NULL,
        weight tinyint DEFAULT '0' NOT NULL,
        region varchar(64) DEFAULT 'left' NOT NULL,
        custom tinyint DEFAULT '0' NOT NULL,
        throttle tinyint DEFAULT '0' NOT NULL,
        visibility tinyint DEFAULT '0' NOT NULL,
        pages text NOT NULL,
        title varchar(64) DEFAULT '' NOT NULL
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)
    print cursor.fetchall()

    cursor.execute("""
        CREATE TABLE boxes (
        bid int NOT NULL auto_increment,
        body longtext,
        info varchar(128) NOT NULL default '',
        format int NOT NULL default '0',
        PRIMARY KEY (bid),
        UNIQUE KEY info (info)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)
    print cursor.fetchall()

    cursor.execute("""
        CREATE TABLE cache (
        cid varchar(255) NOT NULL default '',
        data longblob,
        expire int NOT NULL default '0',
        created int NOT NULL default '0',
        headers text,
        PRIMARY KEY (cid),
        INDEX expire (expire)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)
    print cursor.fetchall()
    
    cursor.execute("""
        CREATE TABLE cache_filter (
        cid varchar(255) NOT NULL default '',
        data longblob,
        expire int NOT NULL default '0',
        created int NOT NULL default '0',
        headers text,
        PRIMARY KEY (cid),
        INDEX expire (expire)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
    """)
    print cursor.fetchall()
    
    cursor.execute("""
        CREATE TABLE cache_menu (
        cid varchar(255) NOT NULL default '',
        data longblob,
        expire int NOT NULL default '0',
        created int NOT NULL default '0',
        headers text,
        PRIMARY KEY (cid),
        INDEX expire (expire)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)
    print cursor.fetchall()
    
    cursor.execute("""
        CREATE TABLE cache_page (
        cid varchar(255) BINARY NOT NULL default '',
        data longblob,
        expire int NOT NULL default '0',
        created int NOT NULL default '0',
        headers text,
        PRIMARY KEY (cid),
        INDEX expire (expire)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)
    print cursor.fetchall()

    cursor.execute("""
        CREATE TABLE comments (
        cid int NOT NULL auto_increment,
        pid int NOT NULL default '0',
        nid int NOT NULL default '0',
        uid int NOT NULL default '0',
        subject varchar(64) NOT NULL default '',
        comment longtext NOT NULL,
        hostname varchar(128) NOT NULL default '',
        timestamp int NOT NULL default '0',
        score mediumint NOT NULL default '0',
        status tinyint unsigned NOT NULL default '0',
        format int NOT NULL default '0',
        thread varchar(255) NOT NULL,
        users longtext,
        name varchar(60) default NULL,
        mail varchar(64) default NULL,
        homepage varchar(255) default NULL,
        PRIMARY KEY (cid),
        KEY lid (nid)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)
    print cursor.fetchall()

    cursor.execute("""
        CREATE TABLE node_comment_statistics (
        nid int unsigned NOT NULL auto_increment,
        last_comment_timestamp int NOT NULL default '0',
        last_comment_name varchar(60) default NULL,
        last_comment_uid int NOT NULL default '0',
        comment_count int unsigned NOT NULL default '0',
        PRIMARY KEY (nid),
        KEY node_comment_timestamp (last_comment_timestamp)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)
    print cursor.fetchall()

    cursor.execute("""
        CREATE TABLE files (
        fid int unsigned NOT NULL default 0,
        nid int unsigned NOT NULL default 0,
        filename varchar(255) NOT NULL default '',
        filepath varchar(255) NOT NULL default '',
        filemime varchar(255) NOT NULL default '',
        filesize int unsigned NOT NULL default 0,
        PRIMARY KEY (fid),
        KEY nid (nid)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)
    print cursor.fetchall()

    cursor.execute("""
        CREATE TABLE file_revisions (
        fid int unsigned NOT NULL default 0,
        vid int unsigned NOT NULL default 0,
        description varchar(255) NOT NULL default '',
        list tinyint unsigned NOT NULL default 0,
        PRIMARY KEY (fid, vid),
        KEY (vid)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)
    print cursor.fetchall()

    cursor.execute("""
        CREATE TABLE filter_formats (
        format int NOT NULL auto_increment,
        name varchar(255) NOT NULL default '',
        roles varchar(255) NOT NULL default '',
        cache tinyint NOT NULL default '0',
        PRIMARY KEY (format),
        UNIQUE KEY (name)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)
    print cursor.fetchall()

    cursor.execute("""
        CREATE TABLE filters (
        format int NOT NULL default '0',
        module varchar(64) NOT NULL default '',
        delta tinyint DEFAULT '0' NOT NULL,
        weight tinyint DEFAULT '0' NOT NULL,
        INDEX (weight)
      ) 
      """)
    print cursor.fetchall()
      
    cursor.execute("""
        CREATE TABLE flood (
        event varchar(64) NOT NULL default '',
        hostname varchar(128) NOT NULL default '',
        timestamp int NOT NULL default '0'
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)
    print cursor.fetchall()

    cursor.execute("""
        CREATE TABLE history (
        uid int NOT NULL default '0',
        nid int NOT NULL default '0',
        timestamp int NOT NULL default '0',
        PRIMARY KEY (uid,nid)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)
    print cursor.fetchall()

    cursor.execute("""
        CREATE TABLE menu (
        mid int unsigned NOT NULL default '0',
        pid int unsigned NOT NULL default '0',
        path varchar(255) NOT NULL default '',
        title varchar(255) NOT NULL default '',
        description varchar(255) NOT NULL default '',
        weight tinyint NOT NULL default '0',
        type int unsigned NOT NULL default '0',
        PRIMARY KEY (mid)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)
    print cursor.fetchall()

    cursor.execute("""
        CREATE TABLE node (
        nid int unsigned NOT NULL auto_increment,
        vid int unsigned NOT NULL default '0',
        type varchar(32) NOT NULL default '',
        title varchar(128) NOT NULL default '',
        uid int NOT NULL default '0',
        status int NOT NULL default '1',
        created int NOT NULL default '0',
        changed int NOT NULL default '0',
        comment int NOT NULL default '0',
        promote int NOT NULL default '0',
        moderate int NOT NULL default '0',
        sticky int NOT NULL default '0',
        PRIMARY KEY  (nid, vid),
        UNIQUE KEY vid (vid),
        KEY node_type (type(4)),
        KEY node_title_type (title, type(4)),
        KEY status (status),
        KEY uid (uid),
        KEY node_moderate (moderate),
        KEY node_promote_status (promote, status),
        KEY node_created (created),
        KEY node_changed (changed),
        KEY node_status_type (status, type, nid),
        KEY nid (nid)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)
    print cursor.fetchall()

    cursor.execute("""
        CREATE TABLE node_access (
        nid int unsigned NOT NULL default '0',
        gid int unsigned NOT NULL default '0',
        realm varchar(255) NOT NULL default '',
        grant_view tinyint unsigned NOT NULL default '0',
        grant_update tinyint unsigned NOT NULL default '0',
        grant_delete tinyint unsigned NOT NULL default '0',
        PRIMARY KEY (nid,gid,realm)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)
    print cursor.fetchall()

    cursor.execute("""
        CREATE TABLE node_revisions (
        nid int unsigned NOT NULL,
        vid int unsigned NOT NULL,
        uid int NOT NULL default '0',
        title varchar(128) NOT NULL default '',
        body longtext NOT NULL,
        teaser longtext NOT NULL,
        log longtext NOT NULL,
        timestamp int NOT NULL default '0',
        format int NOT NULL default '0',
        PRIMARY KEY  (vid),
        KEY nid (nid),
        KEY uid (uid)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)
    print cursor.fetchall()

    cursor.execute("""
        CREATE TABLE node_type (
        type varchar(32) NOT NULL,
        name varchar(255) NOT NULL default '',
        module varchar(255) NOT NULL,
        description mediumtext NOT NULL,
        help mediumtext NOT NULL,
        has_title tinyint unsigned NOT NULL,
        title_label varchar(255) NOT NULL default '',
        has_body tinyint unsigned NOT NULL,
        body_label varchar(255) NOT NULL default '',
        min_word_count smallint unsigned NOT NULL,
        custom tinyint NOT NULL DEFAULT '0',
        modified tinyint NOT NULL DEFAULT '0',
        locked tinyint NOT NULL DEFAULT '0',
        orig_type varchar(255) NOT NULL default '',
        PRIMARY KEY (type)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)
    print cursor.fetchall()

    cursor.execute("""
        CREATE TABLE url_alias (
        pid int unsigned NOT NULL auto_increment,
        src varchar(128) NOT NULL default '',
        dst varchar(128) NOT NULL default '',
        PRIMARY KEY (pid),
        UNIQUE KEY dst (dst),
        KEY src (src)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)
    print cursor.fetchall()

    cursor.execute("""
        CREATE TABLE permission (
        rid int unsigned NOT NULL default '0',
        perm longtext,
        tid int unsigned NOT NULL default '0',
        KEY rid (rid)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)
    print cursor.fetchall()

    cursor.execute("""      
        CREATE TABLE role (
        rid int unsigned NOT NULL auto_increment,
        name varchar(64) NOT NULL default '',
        PRIMARY KEY (rid),
        UNIQUE KEY name (name)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)
    print cursor.fetchall()

    cursor.execute("""
    CREATE TABLE blocks_roles (
        module varchar(64) NOT NULL,
        delta varchar(32) NOT NULL,
        rid int unsigned NOT NULL,
        PRIMARY KEY (module, delta, rid)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)
    print cursor.fetchall()

    cursor.execute("""
      CREATE TABLE sessions (
        uid int unsigned NOT NULL,
        sid varchar(64) NOT NULL default '',
        hostname varchar(128) NOT NULL default '',
        timestamp int NOT NULL default '0',
        cache int NOT NULL default '0',
        session longtext,
        KEY uid (uid),
        PRIMARY KEY (sid),
        KEY timestamp (timestamp)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)
    print cursor.fetchall()

    cursor.execute("""
      CREATE TABLE sequences (
        name varchar(255) NOT NULL default '',
        id int unsigned NOT NULL default '0',
        PRIMARY KEY (name)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)
    print cursor.fetchall()

    cursor.execute("""
      CREATE TABLE node_counter (
        nid int NOT NULL default '0',
        totalcount bigint unsigned NOT NULL default '0',
        daycount mediumint unsigned NOT NULL default '0',
        timestamp int unsigned NOT NULL default '0',
        PRIMARY KEY (nid),
        KEY totalcount (totalcount),
        KEY daycount (daycount),
        KEY timestamp (timestamp)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)
    print cursor.fetchall()

    cursor.execute("""
        CREATE TABLE system (
        filename varchar(255) NOT NULL default '',
        name varchar(255) NOT NULL default '',
        type varchar(255) NOT NULL default '',
        description varchar(255) NOT NULL default '',
        status int NOT NULL default '0',
        throttle tinyint DEFAULT '0' NOT NULL,
        bootstrap int NOT NULL default '0',
        schema_version smallint NOT NULL default -1,
        weight int NOT NULL default '0',
        PRIMARY KEY (filename),
        KEY (weight)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)

    cursor.execute("""
      CREATE TABLE term_data (
        tid int unsigned NOT NULL auto_increment,
        vid int unsigned NOT NULL default '0',
        name varchar(255) NOT NULL default '',
        description longtext,
        weight tinyint NOT NULL default '0',
        PRIMARY KEY (tid),
        KEY vid (vid)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)

    cursor.execute("""
      CREATE TABLE term_hierarchy (
        tid int unsigned NOT NULL default '0',
        parent int unsigned NOT NULL default '0',
        KEY tid (tid),
        KEY parent (parent),
        PRIMARY KEY (tid, parent)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)

    cursor.execute("""
      CREATE TABLE term_node (
        nid int unsigned NOT NULL default '0',
        tid int unsigned NOT NULL default '0',
        KEY nid (nid),
        KEY tid (tid),
        PRIMARY KEY (tid,nid)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)

    cursor.execute("""
      CREATE TABLE term_relation (
        tid1 int unsigned NOT NULL default '0',
        tid2 int unsigned NOT NULL default '0',
        KEY tid1 (tid1),
        KEY tid2 (tid2)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)

    cursor.execute("""
      CREATE TABLE term_synonym (
        tid int unsigned NOT NULL default '0',
        name varchar(255) NOT NULL default '',
        KEY tid (tid),
        KEY name (name(3))
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)

    cursor.execute("""
      CREATE TABLE users (
        uid int unsigned NOT NULL default '0',
        name varchar(60) NOT NULL default '',
        pass varchar(32) NOT NULL default '',
        mail varchar(64) default '',
        mode tinyint NOT NULL default '0',
        sort tinyint default '0',
        threshold tinyint default '0',
        theme varchar(255) NOT NULL default '',
        signature varchar(255) NOT NULL default '',
        created int NOT NULL default '0',
        access int NOT NULL default '0',
        login int NOT NULL default '0',
        status tinyint NOT NULL default '0',
        timezone varchar(8) default NULL,
        language varchar(12) NOT NULL default '',
        picture varchar(255) NOT NULL DEFAULT '',
        init varchar(64) default '',
        data longtext,
        PRIMARY KEY (uid),
        UNIQUE KEY name (name),
        KEY created (created),
        KEY access (access)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)

    cursor.execute("""
      CREATE TABLE users_roles (
        uid int unsigned NOT NULL default '0',
        rid int unsigned NOT NULL default '0',
        PRIMARY KEY (uid, rid)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)

    cursor.execute("""
      CREATE TABLE variable (
        name varchar(48) NOT NULL default '',
        value longtext NOT NULL,
        PRIMARY KEY (name)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)

    cursor.execute("""
      CREATE TABLE vocabulary (
        vid int unsigned NOT NULL auto_increment,
        name varchar(255) NOT NULL default '',
        description longtext,
        help varchar(255) NOT NULL default '',
        relations tinyint unsigned NOT NULL default '0',
        hierarchy tinyint unsigned NOT NULL default '0',
        multiple tinyint unsigned NOT NULL default '0',
        required tinyint unsigned NOT NULL default '0',
        tags tinyint unsigned NOT NULL default '0',
        module varchar(255) NOT NULL default '',
        weight tinyint NOT NULL default '0',
        PRIMARY KEY (vid)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)

    cursor.execute("""
      CREATE TABLE vocabulary_node_types (
        vid int unsigned NOT NULL DEFAULT '0',
        type varchar(32) NOT NULL DEFAULT '',
        PRIMARY KEY (vid, type)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)

    cursor.execute("""
      CREATE TABLE watchdog (
        wid int NOT NULL auto_increment,
        uid int NOT NULL default '0',
        type varchar(16) NOT NULL default '',
        message longtext NOT NULL,
        severity tinyint unsigned NOT NULL default '0',
        link varchar(255) NOT NULL default '',
        location text NOT NULL,
        referer varchar(128) NOT NULL default '',
        hostname varchar(128) NOT NULL default '',
        timestamp int NOT NULL default '0',
        PRIMARY KEY (wid),
        KEY (type)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ 
      """)

        
    print cursor.fetchall()
        
    cursor.close()
    connection.close()
                 

def main():
    # Load deployment configuration
    print "Loading configuration info..."
    config = JsonLoader.jsonLoader('./deploy.json')
    
    CONFIG_SITE = config['site']
    CONFIG_WEBSERVER = config['web_server'] 
    CONFIG_DBSERVER = config['db_server']
    
    # initialize a bunch of "global" variables
    SRC_ROOT = os.path.split(os.getcwd())[0]
    DRUPAL_DEPLOY_PATH = os.path.join(CONFIG_WEBSERVER['htdocs'], CONFIG_SITE['path'])
    DRUPAL_SRC_PATH = os.path.join(SRC_ROOT, config['drupal_src']['location'])


    # Create a new directory containing all Drupal files and directories
    print "Removing the deployment path: ", DRUPAL_DEPLOY_PATH
    if os.path.isdir(DRUPAL_DEPLOY_PATH):
        force_remove(DRUPAL_DEPLOY_PATH)
        
    print "Copying Drupal source to the deployment path "
    shutil.copytree(DRUPAL_SRC_PATH, DRUPAL_DEPLOY_PATH)

    if CONFIG_DBSERVER['password'] == "":
        drupal_db_str = CONFIG_DBSERVER['type'] + '://' + CONFIG_DBSERVER['username'] + '@' + CONFIG_DBSERVER['host'] + '/' + CONFIG_SITE['db']['dbname']
    else:
        drupal_db_str = CONFIG_DBSERVER['type'] + '://' + CONFIG_DBSERVER['username'] + ':' + CONFIG_DBSERVER['password'] + '@' + CONFIG_DBSERVER['host'] + '/' + CONFIG_SITE['db']['dbname']

    print "Database string: ", drupal_db_str

    # Set the db info to Drupal setting file
    update_drupal_dbinfo(DRUPAL_DEPLOY_PATH, drupal_db_str)
    
    # use the mysql admin account in CONFIG_SERVER to setup the db for this drupal installation as specified in CONFIG_SITE
    
    # Create the database 
    connection = MySQLdb. connect (host   = CONFIG_DBSERVER['host'],
                                   user   = CONFIG_DBSERVER['username'],
                                   passwd = CONFIG_DBSERVER['password'])
    cursor = connection.cursor()
    
    command = "DROP DATABASE IF EXISTS `%s` ;" %(CONFIG_SITE['db']['dbname'])
    cursor.execute(command)
    print cursor.fetchall()
    
    command = "CREATE DATABASE `%s` ;" %(CONFIG_SITE['db']['dbname']) 
    cursor.execute(command)
    print cursor.fetchall()
    connection.close()
    
    # Install System module
    install_system(CONFIG_DBSERVER, CONFIG_SITE)
        
    
    print 'Deployment successful'

if __name__ == '__main__':
    main()
    
    
    
# What information need in this config json file?
# * Where to copy/download the Drupal files and directories
# * Where is the target web server path?
# * Where is the database server and permission(user/pass) to create databases.


